import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../views/index.vue";
import store from "../store";
import createWS from "../utils/createWs";

Vue.use(VueRouter);

const routes = [
  {
    path: "/login",
    name: "login",
    meta: {
      notAuth: true,
    },
    component: () => import("../views/login.vue"),
  },
  {
    path: "/",
    name: "Home",
    component: Home,
  },
  {
    path: "/targetWater", //目标水声特征等数据库
    name: "targetWater",
    redirect: "/targetWater/yibiaopan",
    component: (resolve) => require(["@/views/targetWater/index.vue"], resolve),
    // 子路由
    children: [
      {
        path: "yibiaopan",
        name: "yibiaopan",
        component: (resolve) =>
          require(["@/views/targetWater/yibiaopanPage/index.vue"], resolve),
      },
      {
        path: "yuanshujuzidian",
        name: "yuanshujuzidian",
        component: (resolve) =>
          require([
            "@/views/targetWater/yuanshujuzidianPage/index.vue",
          ], resolve),
      },
      {
        path: "shujuku",
        name: "shujuku",
        component: (resolve) =>
          require(["@/views/targetWater/shujukuPage/index.vue"], resolve),
      },
    ],
  },
  {
    path: "/originalStorage", //原始数据存储与管理平台
    name: "originalStorage",
    component: () => import("../views/originalStorage/index.vue"),
  },
  {
    path: "/shujutiqu",
    name: "shujutiqu",
    component: () => import("../views/originalStorage/shujutiqu/index.vue"),
  },
  {
    path: "/analysisTool",
    name: "analysisTool",
    redirect: "/analysisTool/algorithm",
    component: (resolve) =>
      require(["@/views/analysisTool/index.vue"], resolve),
    // 子路由
    children: [
      {
        path: "analysis",
        name: "analysis",
        component: (resolve) =>
          require(["@/views/analysisTool/analysis/index.vue"], resolve),
      },
      {
        path: "algorithm",
        name: "algorithm",
        component: (resolve) =>
          require(["@/views/analysisTool/algorithm/index.vue"], resolve),
      },
      {
        path: "diyalg",
        name: "diyalg",
        component: (resolve) =>
          require(["@/views/analysisTool/diyalg/index.vue"], resolve),
      },
    ],
  },
  {
    path: "/data_cleaning", //数据清洗与元数据提取网关
    name: "data_cleaning",
    redirect: "/data_cleaning/shaixuanronghe",
    component: (resolve) =>
      require(["@/views/dataCleaning/index.vue"], resolve),
    // 子路由
    children: [
      {
        path: "shaixuanronghe",
        name: "shaixuanronghe",
        component: (resolve) =>
          require(["@/views/dataCleaning/shaixuanronghe/index.vue"], resolve),
      },
      {
        path: "guanlianpanzheng",
        name: "guanlianpanzheng",
        component: (resolve) =>
          require(["@/views/dataCleaning/guanlianpanzheng/index.vue"], resolve),
        props: true,
      },
      {
        path: "suanfashengji",
        name: "suanfashengji",
        component: (resolve) =>
          require(["@/views/dataCleaning/suanfashengji/index.vue"], resolve),
        meta: {
          showbutton: true,
        },
      },
    ],
  },
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  scrollBehavior() {
    //切换路由滚动条置顶
    return {
      x: 0,
      y: 0,
    };
  },
  routes,
});
const VueRouterPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(to) {
  return VueRouterPush.call(this, to).catch((err) => err);
};

router.beforeEach((to, from, next) => {
  if (to.meta.notAuth) {
    next();
  } else {
    if (store.state.token) {
      next();
      if (!store.state.wsInstance) {
        if (store.state.wsInstance?.readyState !== WebSocket.OPEN) {
          createWS();
        }
      } else {
        if (store.state.wsInstance?.readyState !== WebSocket.OPEN) {
          createWS();
        }
      }
    } else {
      next({
        path: "/login",
        query: { redirect: to.fullPath },
      });
      // window.location.refresh();
    }
  }
});

export default router;
